package jeeobserver.server.database;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import jeeobserver.server.Statistics.Element;
import jeeobserver.server.Statistics.Principal;

class DatabaseFileElements<T extends DatabaseFileElements.DatabaseElementEntity> extends DatabaseFile.DatabaseFileVariableRecord {

	public DatabaseFileElements(String path, Class<T> type) throws IOException {
		super(path + ".dat", type);
	}

	public void merge(Map<Integer, DatabaseElementEntity> elementsMap) throws IOException {

		this.reset();

		Object entity;

		while ((entity = this.read()) != null) {
			elementsMap.remove(((DatabaseElementEntity) entity).getElement().getId());
		}

		//If element not exists insert
		for (int elementId : elementsMap.keySet()) {
			this.append((DatabaseEntity) elementsMap.get(elementId));
		}
	}

	public abstract static class DatabaseElementEntity implements DatabaseEntity {

		public static final byte STATUS_DELETED = 1;

		public static final byte STATUS_ACTIVE = 0;

		protected byte status;

		public byte getStatus() {
			return status;
		}

		public void setStatus(byte status) {
			this.status = status;
		}

		public abstract Element getElement();
	}
}